package com.dldata.devtools30.utils.dbtable;


import com.dldata.devtools30.entity.DevtModelMetadataFieldEntity;
import com.dldata.devtools30.entity.DevtModelMetadataTableEntity;
import com.dldata.devtools30.repository.common.DlBaseJdbcTemplate;
import com.dldata.devtools30.utils.DBTypeUtil;
import com.dldata.devtools30.utils.dbtable.imp.DbTableServiceMysqlImpl;
import com.dldata.devtools30.utils.dbtable.imp.DbTableServiceOracleImpl;
import com.dldata.devtools30.utils.dbtable.imp.DbTableServiceSQLServerImpl;
import lombok.extern.slf4j.Slf4j;

import java.util.List;

@Slf4j
/**
 * 对于修改数据库的字段，考虑各种数据库的情况，字段名称全部、类型修改成大写
 */
public class DbTableManager implements DbTableServiceIV2{

	private DbTableServiceIV2 dbTableServiceI;

	private DlBaseJdbcTemplate dlBaseJdbcTemplate;

	public DbTableManager(DlBaseJdbcTemplate dlBaseJdbcTemplate, DBTypeUtil.DBTYPE dbtype) {
		this.dlBaseJdbcTemplate = dlBaseJdbcTemplate;
		if(DBTypeUtil.DBTYPE.mysql.equals(dbtype)){
			dbTableServiceI=new DbTableServiceMysqlImpl(dlBaseJdbcTemplate);
		}else if(DBTypeUtil.DBTYPE.sqlserver.equals(dbtype)){
			dbTableServiceI=new DbTableServiceSQLServerImpl(dlBaseJdbcTemplate);
		}else if(DBTypeUtil.DBTYPE.oracle.equals(dbtype)){
			dbTableServiceI=new DbTableServiceOracleImpl(dlBaseJdbcTemplate);
		}else{
			throw new RuntimeException(" not support dbtype."+dbtype);
		}
	}



	public DbTableManager(DlBaseJdbcTemplate dlBaseJdbcTemplate) {
		this.dlBaseJdbcTemplate = dlBaseJdbcTemplate;
		DBTypeUtil.DBTYPE dbtype= DBTypeUtil.getDbtype(dlBaseJdbcTemplate.getDataSource());
		if(DBTypeUtil.DBTYPE.mysql.equals(dbtype)||DBTypeUtil.DBTYPE.h2.equals(dbtype)){
			dbTableServiceI=new DbTableServiceMysqlImpl(dlBaseJdbcTemplate);
		}else if(DBTypeUtil.DBTYPE.sqlserver.equals(dbtype)){
			dbTableServiceI=new DbTableServiceSQLServerImpl(dlBaseJdbcTemplate);
		}else if(DBTypeUtil.DBTYPE.oracle.equals(dbtype)){
			dbTableServiceI=new DbTableServiceOracleImpl(dlBaseJdbcTemplate);
		}else{
			throw new RuntimeException(" not support dbtype."+dbtype);
		}
	}

	/**
	 * 1、表是否存在
	 * 1.1如果不存在就直接创建
	 * 2、查询数据行
	 * 2.1如果为0就删除表后创建
	 * 3.对比字段更新字段
	 * @param table
	 * @param formFieldEntities
	 */
	public void  dbsyncTable(DevtModelMetadataTableEntity table, List<DevtModelMetadataFieldEntity> formFieldEntities){
		Boolean existsSql=dbTableServiceI.isExistTable(table.getCode());
		if(!existsSql){
			dbTableServiceI.createNewTable(table, formFieldEntities,false);
			return;
		}
		Integer queryRecordCount=dbTableServiceI.queryRecordCount(table.getCode());
		if(queryRecordCount==null||queryRecordCount==0){
			dbTableServiceI.createNewTable(table, formFieldEntities,true);
			return;
		}
		dbTableServiceI.updateTable(table,formFieldEntities);
	}

	/**
	 * 获取表格字段
	 * @param table
	 * @return SQL
	 */
	public List<DevtModelMetadataFieldEntity> queryTableFields(DevtModelMetadataTableEntity table){
		return dbTableServiceI.queryTableFields(table);
	}

	/**
	 * 1、表是否存在
	 * 1.1如果不存在就直接创建
	 * 2、查询数据行
	 * 2.1如果为0就删除表后创建
	 * 3.对比字段更新字段
	 * @param table
	 * @param formFieldEntities
	 */
	public String dbsyncTableSql(DevtModelMetadataTableEntity table, List<DevtModelMetadataFieldEntity> formFieldEntities){
		Boolean existsSql=dbTableServiceI.isExistTable(table.getCode());
		if(!existsSql){
			return dbTableServiceI.createTableSql(table, formFieldEntities);
		}
//		Integer queryRecordCount=dbTableServiceI.queryRecordCount(table.getCode());
//		if(queryRecordCount==null||queryRecordCount==0){
//			return dbTableServiceI.createTableSql(table, formFieldEntities);
//		}
		return  dbTableServiceI.updateTableSql(table, formFieldEntities);
	}

	public static enum FIELDTYPE{

		STRING("string","字符串",Integer.MAX_VALUE),INTEGER("integer","整数",0),DATETIME("datetime","日期时间",1),DATE("date","日期",2),
		DECEMEMOL("decimal","小数",3),CASH("cash","货币",4),BOOLEAN("boolean","布尔型",Integer.MIN_VALUE);

		private String code;

		private String lable;

		public Integer sort;

		private FIELDTYPE(String code,String lable,Integer sort){
			this.sort=sort;
			this.code=code;
			this.lable=lable;
		}

		public String getCode() {
			return code;
		}
		public String getLable() {
			return lable;
		}

		public static FIELDTYPE valueOfCode(String code){
			FIELDTYPE[] items= FIELDTYPE.values();
			for (int i = 0; i < items.length; i++) {
				FIELDTYPE item = items[i];
				if(item.getCode().equals(code)){
					return item;
				}
			}
			return  null;
		}
	}


	@Override
	public String createTableSql(DevtModelMetadataTableEntity table, List<DevtModelMetadataFieldEntity> formFieldEntities) {
		return dbTableServiceI.createTableSql(table,formFieldEntities);
	}

	@Override
	public String updateTableSql(DevtModelMetadataTableEntity table, List<DevtModelMetadataFieldEntity> formFieldEntities) {
		return dbTableServiceI.updateTableSql(table,formFieldEntities);
	}

	@Override
	public void createNewTable(DevtModelMetadataTableEntity table, List<DevtModelMetadataFieldEntity> formFieldEntities, boolean isdrop) {
		dbTableServiceI.createNewTable(table,formFieldEntities,isdrop);
	}

	@Override
	public Boolean isExistTable(String tableName) {
		return dbTableServiceI.isExistTable(tableName);
	}

	@Override
	public Integer queryRecordCount(String tableName) {
		return dbTableServiceI.queryRecordCount(tableName);
	}

	@Override
	public void updateTable(DevtModelMetadataTableEntity table, List<DevtModelMetadataFieldEntity> formFieldEntities) {
		dbTableServiceI.updateTable(table,formFieldEntities);
	}
}
